package com.njegovanobradovic.icet.pages;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.njegovanobradovic.icet.facade.Controller;
import com.njegovanobradovic.icet.metamodel.User;
import com.njegovanobradovic.icet.metamodel.beans.UserBean;

public class Registration {
	@Property
	@Persist
	private User newUser;
	
	@Inject
	private Controller controller;
	
	@InjectPage
	private StatusPage statusPage;

	@Persist
	private String message;
	
	@Property
	private String rePassword;
	
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public void onActivate(){
		if (newUser==null) {
			newUser = new UserBean();
		}
	}

	@Log
	public Object onSuccess(){
		try {
			controller.signUpUser(newUser,rePassword);
			setMessage(controller.getMessage());
			statusPage.setMessage(controller.getMessage());
			controller.userLogin(newUser.getUsername(), newUser.getPassword());
			return statusPage;
		} catch (Exception e) {
			return Home.class;
		}
		
	}
	
	public boolean getLoggedUser(){
		if (!controller.isLoggedIn()) {
			return true;
		}
		return false;
	}
}
